import request from '@/utils/request'
import { CategoryResponseData, AttrResponseData, Attr } from './type'

enum ATTR_API_URL {
  /**
   * 获取一级路由分类接口地址
   */
  C1_URL = '/admin/product/getCategory1',

  /**
   * 获取二级路由分类接口地址
   */
  C2_URL = '/admin/product/getCategory2',

  /**
   * 获取三级路由分类接口地址
   */
  C3_URL = '/admin/product/getCategory3',

  /**
   * 获取分类下已有的属性及属性值
   */
  ATTR_URL = '/admin/product/attrInfoList',

  /**
   * 添加/修改属性
   */
  ADD_OR_UPDATE_ATTR_URL = '/admin/product/saveAttrInfo',

  /**
   * 删除属性
   */
  DETELE_ATTR_URL = '/admin/product/deleteAttr',
}

/**
 * 获取一级分类的接口方法
 * @returns Promise<CategoryResponseData>
 */
export const reqC1 = (): Promise<CategoryResponseData> =>
  request.get<any, CategoryResponseData>(ATTR_API_URL.C1_URL)

/**
 * 获取二级分类的接口方法
 * @param category1Id
 * @returns Promise<CategoryResponseData>
 */
export const reqC2 = (category1Id: number): Promise<CategoryResponseData> =>
  request.get<any, CategoryResponseData>(ATTR_API_URL.C2_URL + `/${category1Id}`)

/**
 * 获取三级分类的接口方法
 * @param category2Id
 * @returns Promise<CategoryResponseData>
 */
export const reqC3 = (category2Id: number): Promise<CategoryResponseData> =>
  request.get<any, CategoryResponseData>(ATTR_API_URL.C3_URL + `/${category2Id}`)

/**
 * 获取已有属性及属性值的接口方法
 * @param category1Id
 * @param category2Id
 * @param category3Id
 * @returns Promise<AttrResponseData>
 */
export const reqAttr = (
  category1Id: string | number,
  category2Id: string | number,
  category3Id: string | number,
): Promise<AttrResponseData> =>
  request.get<any, AttrResponseData>(
    ATTR_API_URL.ATTR_URL + `/${category1Id}/${category2Id}/${category3Id}`,
  )

/**
 * 添加或修改属性的接口方法
 * @param data
 * @returns Promise<any>
 */
export const reqAddorUpdateAttr = (data: Attr): Promise<any> =>
  request.post<any, any>(ATTR_API_URL.ADD_OR_UPDATE_ATTR_URL, data)

/**
 * 删除已有属性的接口方法
 * @param id
 * @returns Promise<any>
 */
export const reqDeleteAttr = (id: number): Promise<any> =>
  request.delete<any, any>(ATTR_API_URL.DETELE_ATTR_URL + `/${id}`)
